#include "IwGx.h"

#include "DailyReportDataProvider.h"


// class DailyReportModel
// -------------------------------------------------------------------------
DailyReportModel::DailyReportModel()
{
	TaskName = new char[255];
	BeginDate = new char[255];
	StartTime = new char[255];
	EndTime = new char[255];
	Description = new char[255];

	Duration = 0;
}
DailyReportModel::DailyReportModel(int pRowId, char* pTaskName, char* pBeginDate, char* pStartTime, char* pEndTime, int pDuration)
{
	RowId = pRowId;
	TaskName = pTaskName;
	BeginDate = pBeginDate;
	StartTime = pStartTime;
	EndTime = pEndTime;
	Duration = pDuration;
	Description = new char[255];
}
// -------------------------------------------------------------------------
DailyReportModel::~DailyReportModel()
{
	if(TaskName)
		delete[] TaskName;
	if(BeginDate)
		delete[] BeginDate;
	if(StartTime)
		delete[] StartTime;
	if(EndTime)
		delete[] EndTime;
	if(Description)
		delete[] Description;
}



// class DailyReportDataProvider
// -------------------------------------------------------------------------
DailyReportDataProvider::DailyReportDataProvider()
	:DB_FILE_NAME("sqlite.sdb"),dbProvider(NULL)
{
	if (s3eFileCheckExists(DB_FILE_NAME) == S3E_TRUE)
	{
		dbProvider = new SqlDatabase(DB_FILE_NAME, false);
	}
}
// -------------------------------------------------------------------------
DailyReportDataProvider::~DailyReportDataProvider()
{
	if(dbProvider)
	{
		dbProvider->close();
		delete dbProvider;
		dbProvider = NULL;
	}
}

// -------------------------------------------------------------------------
int DailyReportDataProvider::InsertData(DailyReportModel* pDailyReportModel)
{
	// Insert Data.
	SqlStatement insert = dbProvider->sqlCompile("insert into DailyReport (TaskName, BeginDate, StartTime, EndTime, Duration) values (?, ?, ?, ?, ?);");
	insert.bind(pDailyReportModel->TaskName)
		.bind(pDailyReportModel->BeginDate)
		.bind(pDailyReportModel->StartTime)
		.bind(pDailyReportModel->EndTime)
		.bind(pDailyReportModel->Duration)
		.execute();
	insert.destroy();
	// Get last RowId.
	SqlStatement select = dbProvider->sqlQuery("select RowId from DailyReport order by RowId desc");
	select.execute();

	int rowId = -1;
	if(select.hasRow())
	{
		const SqlStatement::ResultRow& row = select.currentRow();
		rowId = row.getIntField("RowId");
	}
	select.destroy();

	return rowId;
}
// -------------------------------------------------------------------------
// Update data of selected item (rowId) in Grid.
void DailyReportDataProvider::UpdateDataWithoutDuration(DailyReportModel* pDailyReportModel)
{
	SqlStatement update = dbProvider->sqlCompile("update DailyReport set TaskName = ?, BeginDate = ?, StartTime = ?, EndTime = ?, Description = ? where RowId = ?;");
	update.bind(pDailyReportModel->TaskName)
		.bind(pDailyReportModel->BeginDate)
		.bind(pDailyReportModel->StartTime)
		.bind(pDailyReportModel->EndTime)
		.bind(pDailyReportModel->Description)
		.bind(pDailyReportModel->RowId)
		.execute();
	update.destroy();
}
// -------------------------------------------------------------------------
void DailyReportDataProvider::UpdateDataDone(DailyReportModel* pDailyReportModel)
{
	SqlStatement update = dbProvider->sqlCompile("update DailyReport set EndTime = ?, Duration = ? where RowId = ?;");
	update.bind(pDailyReportModel->EndTime)
		.bind(pDailyReportModel->Duration)
		.bind(pDailyReportModel->RowId)
		.execute();
	update.destroy();
}
// -------------------------------------------------------------------------
// Delete data of selected item (rowId) in Grid.
void DailyReportDataProvider::DeleteData(const int pRowId)
{
	SqlStatement del = dbProvider->sqlCompile("delete from DailyReport where RowId = ?;");
	del.bind(pRowId).execute();
	del.destroy();
}
// -------------------------------------------------------------------------
// Get data by RowId
void DailyReportDataProvider::GetDataByRowId(DailyReportModel* pDailyReportModel, const int pRowId)
{
	SqlStatement select = dbProvider->sqlQuery("select RowId, TaskName, BeginDate, StartTime, EndTime, Description, Duration from DailyReport where RowId=?");
	select.bind(pRowId);
	select.execute();

	if(select.hasRow())
	{
		const SqlStatement::ResultRow& row = select.currentRow();
		pDailyReportModel->RowId = pRowId;
		strcpy(pDailyReportModel->TaskName,row.getStringField("TaskName"));
		strcpy(pDailyReportModel->BeginDate,row.getStringField("BeginDate"));
		strcpy(pDailyReportModel->StartTime,row.getStringField("StartTime"));
		strcpy(pDailyReportModel->EndTime,row.getStringField("EndTime"));
		strcpy(pDailyReportModel->Description,row.getStringField("Description"));
		pDailyReportModel->Duration = row.getIntField("Duration");
	}
	select.destroy();
}

// -------------------------------------------------------------------------
// Select all order by RowId
void DailyReportDataProvider::GetsAllRowId(CIwArray<int32>* pRowIds)
{
	SqlStatement select = dbProvider->sqlQuery("select RowId, TaskName, BeginDate, StartTime, EndTime, Duration from DailyReport order by RowId desc");
	if(select.hasRow())
	{
		do
		{
			const SqlStatement::ResultRow& row = select.currentRow();
			int32 rowId = row.getIntField("RowId");
			pRowIds->push_back(rowId);
		}while(select.nextRow());
	}
	select.destroy();
}